package com.gds.wiki.cloud.admin.server.modular.sys.controller;

import com.gds.wiki.cloud.admin.client.sys.model.Dept;
import com.gds.wiki.cloud.admin.client.sys.model.query.DeptQuery;
import com.gds.wiki.cloud.admin.server.modular.sys.service.DeptService;
import com.gds.wiki.cloud.framework.common.dto.ResponseDto;
import com.gds.wiki.cloud.framework.common.exception.BizExceptionEnum;
import com.gds.wiki.cloud.framework.web.common.BaseController;
import com.gds.wiki.cloud.oauth2.admin.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author:wiki
 * @Date:Create in 2018/7/20 0020 上午 10:29
 */

@Api(value = "DeptController", tags = "用户中心微服务-部门管理")
@RestController
@RequestMapping("dept")
@Validated
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @PostMapping("list")
    @ApiOperation(value = "获取部门列表",notes = "获取部门列表",httpMethod = "POST")
    @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header")
    public ResponseDto list(@RequestBody @Validated DeptQuery deptQuery) throws Exception {
        ResponseDto rs=new ResponseDto(BizExceptionEnum.SUCCESSFUL_OPERATION);
        Long deptId=SecurityUtil.getSecurityUser().getDeptId();
        if(deptQuery.getDeptId()==null){
            deptQuery.setDeptId(deptId);
        }else {
            if(deptId!=0){
                List<Long> list=deptService.deptIdsByPid(deptId);
                if(!list.contains(deptQuery.getDeptId())){
                    rs.setCode(BizExceptionEnum.FAIL_OPERATION.getCode());
                    rs.setMesg("查看部门权限不足");
                    return  rs;
                }
            }
        }
        rs.setData(deptService.treeByPid(deptQuery));
        return rs;
    }

    @PostMapping
    @ApiOperation(value = "新增或修改部门",notes = "新增或修改部门",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "Dept", value = "部门", required = true, dataType = "Dept", paramType = "body")
    })
    public ResponseDto<Dept> save(@RequestBody @Validated Dept dept){
        ResponseDto rs=new ResponseDto<Dept>(BizExceptionEnum.SUCCESSFUL_OPERATION);
        Long deptId=SecurityUtil.getSecurityUser().getDeptId();
            if(deptId!=0){
                List<Long> list=deptService.deptIdsByPid(deptId);
                if(!list.contains(dept.getPid())){
                    rs.setCode(BizExceptionEnum.FAIL_OPERATION.getCode());
                    rs.setMesg("查看部门权限不足");
                    return  rs;
                }
            }
        rs.setData(deptService.saveOrUpdate(dept));
        return rs;
    }
    @PostMapping("del")
    @ApiOperation(value = "删除部门",notes = "删除部门",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "id", value = "部门", required = true, dataType = "Long", paramType = "query")
    })
    public ResponseDto<Dept> del(@NotNull(message = "部门id不能为空") Long id){
        ResponseDto rs=new ResponseDto<Dept>(BizExceptionEnum.SUCCESSFUL_OPERATION);
        Dept Dept=new Dept();
        Dept.setId(id);
        Dept.setStatus(-1);
        deptService.updateById(Dept);
        return rs;
    }
}
